# -*- coding: utf-8 -*-
from lxml import etree
from urlparse import urljoin
import urllib
import cStringIO
import os
import datetime
import json
import mechanize


cookie_str = ''

def getHtml(url):
    
    
    opener = mechanize.build_opener(mechanize.HTTPRedirectHandler)
    opener.addheaders = [("User-Agent", "Mozilla/5.0 (Windows NT 6.2; rv:18.0) Gecko/20100101 Firefox/18.0"),
                         ("Cookie", cookie_str),
                         ("Connection", "keep-alive")]
    mechanize.install_opener(opener)
    res = mechanize.urlopen(url)
    html = res.read()
    info = res.info()
    print info
    if 'Set-Cookie' in info:
        global cookie_str
        cookie_str = info['Set-Cookie']
    return html

def buildTree(html):
    
    parser = etree.HTMLParser(encoding='utf-8')
    tree = etree.parse(cStringIO.StringIO(html), parser=parser)
    return tree


def getListChapter():
    print '> getListChapter'
    tree = buildTree(getHtml(URL))
    data = []
    for node in tree.xpath("//table[@class='mytable']//th/a"):
        chapter = node.text
        link = node.get('href', '').strip()
        if link == '' or chapter == None: continue
        link = urljoin(URL, link)
        data.append({'title': chapter, 'url': link})
    return data
    
def getImage(url):
    print '> getImage from URL: {0}'.format(url)
    html = getHtml(url)
    tree = buildTree(html)
    data = []
#    print html
    print len(tree.xpath("//ul[@id='portfolio']"))
    for node in tree.xpath("//ul[@id='portfolio']//img"):
        src = node.get('src', '')
        if src == '': continue
        src = urljoin(url, src)
        print src
        data.append(src)
    return data

def process():
    
    chapters = getListChapter()
    data = []
    for chapter in chapters:
        images = getImage(chapter['url'])
        data.append({'title': chapter['title'], 'images': images})
    output = json.dumps(data)
    print output


# bat dau phan body cua chuong trinh
if __name__ == '__main__':
    
    URL = 'http://manga24h.com/81/Dragon-Ball-Bay-Vien-Ngoc-Rong.html'
    getHtml('http://manga24h.com')
    process()

    

#    getListChapter()
#    getImage('http://manga24h.com/81/Dragon-Ball-Bay-Vien-Ngoc-Rong.html')
    print '> Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)


